package com.lanou.musicbox.adapter.onlinemusic;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.activity.MainActivity;
import com.lanou.musicbox.bean.Recommend;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/30.
 */
public class RecommendViewPager extends PagerAdapter {
    private Context context;
    private ArrayList<Recommend.PicEntity> data;
    private ImageLoader imageLoader;
    private MainActivity activity;

    public RecommendViewPager(Context context, ArrayList<Recommend.PicEntity> data) {
        this.context = context;
        this.data = data;
        activity = (MainActivity) context;
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();//初始化
    }

    @Override
    public int getCount() {

        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //页卡增加
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((data.get(position).getCode().length() == 4)) {

                    activity.setFragment(position, data);
                }

            }
        });


        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(data.get(position % data.size()).getRandpic(), imageListener);

        container.addView(imageView);

        return imageView;

    }

    //页卡删除
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }
}
